//
//  MTModalTransition.swift
//  MiTao
//
//  Created by zhang qiang on 2018/12/6.
//  Copyright © 2018 DDC. All rights reserved.
//

import Foundation


class MTModalTransition: NSObject, UIViewControllerTransitioningDelegate {
    
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return OverlayPresentationController.init(presentedViewController: presented, presenting: presenting)
    }
    
}


class OverlayPresentationController: UIPresentationController {
    public var dimmingView: UIView?
    
    override func presentationTransitionWillBegin() {
        self.dimmingView = UIView()
        
        self.containerView?.addSubview(self.dimmingView!)
        
        let toViewWidth = self.containerView!.mt_width
        let toViweHeight = self.containerView!.mt_height
        
        self.dimmingView?.backgroundColor = .black
        self.dimmingView?.alpha = 0.5
        self.dimmingView?.center = (self.containerView?.center)!
        self.dimmingView?.bounds = CGRect(x: 0, y: 0, width: toViewWidth, height: toViweHeight)
        
        self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
            self.dimmingView?.bounds = self.containerView!.bounds
        }, completion: nil)
    }
    
    override func dismissalTransitionWillBegin() {
        self.presentingViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
            self.dimmingView?.alpha = 0
        }, completion: nil)
    }
}
